<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>

<%
            if (session.getAttribute("user") == null) {
                session.setAttribute("user", new User());
            }

            String p = request.getParameter("page");
            boolean loggedIn = ((User) session.getAttribute("user")).username != null;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="alternate" type="application/rss+xml" title="RSS" href="/feed.xml" />
        <title>Hello World -
            <%
            if (p == null) {
                out.print(" Main Page");
            } else if (p.compareTo("login") == 0) {
                out.print(" Login");
            } else if (p.compareTo("register") == 0) {
                out.print(" Register");
            } else if (p.compareTo("logout") == 0) {
                out.print(" Logout");
            } else if (p.compareTo("profile") == 0) {
                out.print(" Profile");
            } else if (p.compareTo("addTrip") == 0) {
                out.print(" Add a new trip");
            } else if (p.compareTo("trip") == 0) {
                out.print(" Trip");
            } else if (p.compareTo("triplocation") == 0) {
                out.print(" Trip location");
            } else if (p.compareTo("addContent") == 0) {
                out.print(" Add content");
            } else {
                out.print(" 404 millionbillion Page not found");
            }
            %>
        </title>
        <link rel="stylesheet" type="text/css" href="style.css" />  
        <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
        <script type="text/javascript">
            function lookup(inputString) {
                if(inputString.length == 0) {
                    $("#suggestions").hide();
                } else {
                    $.post("users", {queryString: "" + inputString + ""}, function(data){
                        if(data.length > 0) {
                            $("#suggestions").show();
                            $("#autoSuggestionsList").html(data);
                        }
                    });
                }
            } 
        
            function fill(thisValue) {
                $("#inputString").val(thisValue);
                $("#inputString").focus();
                $("#goToUser").attr("action", "index.jsp?page=profile&user=" + "" + thisValue + "");
                if(thisValue != "" && thisValue != undefined)
                    setTimeout("$(\"#goToUser\").submit();", 50);
                setTimeout("$(\"#suggestions\").hide();", 100);
            }
            
            function focusTheShizzle() {
                if(document.getElementById('firstFocus'))
                    document.getElementById('firstFocus').focus();
            }
            window.onload = focusTheShizzle;        
            
        </script>
        
        <%
            if (p != null && p.compareTo(
                    "editTrips") == 0) {
                out.print("<script type=\"text/javascript\" src=\"editTrips.js\"></script>");
            }
        %>        
    </head>
    <% if (p == null || p.compareTo(
                    "trip") == 0) {%>
    <body onload ="initialize()" onunload="GUnload()">
    <% } else {%>
    <body> 
        <%}%>
        <div id="main">
            <%
            Layout ll = new Layout(session);
            out.print(ll.Header());
            if (p == null) {
            %>
            <%@include file="map.jsp"%>
            <% } else if (p.compareTo(
                    "login") == 0) {%>
            <%@include file="login.jsp"%>
            <% } else if (p.compareTo(
                    "register") == 0) {%>
            <%@include file="register.jsp"%>
            <% } else if (p.compareTo(
                    "profile") == 0) {%>
            <%@include file="profile.jsp"%>
            <% } else if (p.compareTo(
                    "addTrip") == 0) {%>
            <%@include file="addTrip.jsp"%>    
            <% } else if (p.compareTo(
                    "logout") == 0) {%>
            <%@include file="logout.jsp"%>                        
            <% } else if (p.compareTo(
                    "trip") == 0) {
                TripManager temp = new TripManager(DBInfo.getConnection());

                if (((User) session.getAttribute("user")).username != null && temp.isOwnerOf(Integer.parseInt(request.getParameter("tripid")), ((User) session.getAttribute("user")).username)) {%>
            <%@include file="tripAdmin.jsp"%>
            <% } else if (!(temp.getTripName(Integer.parseInt(request.getParameter("tripid")))).equals("")) {%>
            <%@include file="trip.jsp"%>
            <% } else {
                    out.print("<h1 class=\"nice\">Trip not found.</h1>");
                }
            } else if (p.compareTo(
                    "triplocation") == 0) {%>
            <%@ include file="triplocation.jsp" %>
            <% } else if (p.compareTo(
                    "addContent") == 0) {%>
            <%@ include file="addContent.jsp" %>
            <% } else {
                out.print("<h1 class=\"nice\">Page not found.</h1>");
            }
            out.print(ll.Footer());
            out.print(ll.Navigation());
            %>
        </div>
    </body>
</html>
